import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_theme/theme.dart';
import 'package:flutter_theme/theme_utils.dart';
import 'package:vibration/vibration.dart';
import 'package:flutter_lang/flutter_lang.dart';

import '../../module_config.dart';

class NumberKeyboradWidget extends StatefulWidget {
  final onTap;

  NumberKeyboradWidget({Key key, @required this.onTap}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _NumberKeyboradWidgetState();
}

class _NumberKeyboradWidgetState extends State<NumberKeyboradWidget> {
  List number = [];
  List outPutNumber = [];
  String outPutStr = '';
  String languageRet =
      '${WXLang.$t("authentication_delete_keyboard", package: ModuleConfig.moduleName)}';

  @override
  void initState() {
    super.initState();
    for (var i = 1; i <= 9; i++) {
      number.add(i);
    }

    for (var i = 0; i < number.length; i++) {
      int random = Random().nextInt(number.length);
      dynamic t = number[i];
      number[i] = number[random];
      number[random] = t;
    }

    number.add('C');
    number.add('0');
    number.add(languageRet);

    int random = Random().nextInt(10);
    dynamic tmF = number[10];
    number[10] = number[random];
    number[random] = tmF;
  }

  void _emmitEvent() {
    widget.onTap(outPutStr);
    print('_emmitEvent$outPutStr');
    if (outPutNumber.length == 6) {
      outPutNumber?.clear();
    }
  }

  void _handlerResult(numberItem) {
    // if (numberItem == '.') {
    //   var temp = outPutNumber.contains('.');
    //   if (temp || outPutNumber.length == 0) {
    //     return;
    //   } else {
    //     outPutNumber.add(numberItem);
    //   }
    // } else {
    //   outPutNumber.add(numberItem);
    // }

    if (numberItem == 'C') {
      // var temp = outPutNumber.contains('.');
      // if (temp || outPutNumber.length == 0) {
      //   return;
      // } else {
      //   outPutNumber.add(numberItem);
      // }
      outPutNumber.clear();
    } else {
      outPutNumber.add(numberItem);
    }

    setState(() {
      outPutStr = outPutNumber.join();
    });
    _emmitEvent();
  }

  void _handlerDelNumber() {
    if (outPutNumber.length == 0) {
      return;
    }
    outPutNumber.removeAt(outPutNumber.length - 1);
    setState(() {
      outPutStr = outPutNumber.join();
    });
    _emmitEvent();
  }

  Widget _buildKeysItem(index) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(5.0)),
        color: Colors.white,
      ),
      width: (MediaQuery.of(context).size.width / 3 - 8.0) - 8.0,
      height: 44,
      child: Material(
        color: WXTheme.$c("authentication_number_keyboard_keys_color"),
        child: InkWell(
          highlightColor: Colors.grey[600],
          onTap: () {
            Vibration.vibrate(duration: 50);
            if (index != languageRet) {
              _handlerResult(index);
            } else {
              _handlerDelNumber();
            }
          },
          child: Center(
            child: Text(
              index.toString(),
              style: TextStyle(
                  fontSize: 14,
                  color: WXTheme.$c("authentication_number_keyboard_key_text_color")),
            ),
          ),
        ),
      ),
    );
  }

  List<Widget> _buildWidgetList() {
    return number.map((e) {
      return _buildKeysItem(e);
    }).toList();
  }

  Widget _buildKeys(context) {
    return Container(
      height: 44 * 4 +
          40 +
          (MediaQuery.of(context).padding.bottom > 0 ? 44.0 : 0.0),
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.only(
          left: 8,
          right: 8,
          top: 8,
          bottom: (MediaQuery.of(context).padding.bottom > 0 ? 44.0 : 8.0)),
      color: WXTheme.$c("number_keyboard_background"),
      child: Center(
        child: Wrap(
          alignment: WrapAlignment.spaceBetween,
          spacing: 8.0,
          runSpacing: 8.0,
          children: _buildWidgetList(),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildKeys(context);
  }
}
